import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# 题3：使用交叉表计算星期与股票涨跌幅的关系
# 读取数据
df = pd.read_csv('data/stock.csv', usecols=['open', 'close', 'p_change'])
# 按照索引日期排序
df = df.sort_index()
# 1、先把对应的日期找到星期几
date = pd.to_datetime(df.index).weekday
df['week'] = date
# 2、假如把p_change按照大小去分个类0为界限
df['posi_neg'] = np.where(df['p_change'] > 0, 1, 0)
# 通过交叉表找寻两列数据的关系
count = pd.crosstab(df['week'], df['posi_neg'])
# 算数运算，先求和
sum = count.sum(axis=1).astype(np.float32)
# 进行相除操作，得出比例
pro = count.div(sum, axis=0)
# 画图查看
pro.plot(kind='bar', stacked=True)
plt.show()
